package com.example.shellshopping.fpage;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.shellshopping.R;
import com.example.shellshopping.goods.TypeBean;
import com.example.shellshopping.utils.URLUtils;
import com.squareup.picasso.Picasso;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class FPtypeAdapter extends BaseAdapter {
    Context context;
    List<TypeBean.DataBean>list;
    public FPtypeAdapter(Context context,List<TypeBean.DataBean>allList,int page,int pagesize){
        this.context = context;
        list = new ArrayList<>();
        //0  0~7    1  8~15   2 16~23
        int start = page*pagesize;
        int end = (page+1)*pagesize-1;
        if (end>allList.size()-1) {
            end = allList.size()-1;
        }
        for(int i = start;i<=end;i++) {
            TypeBean.DataBean bean = allList.get(i);
            list.add(bean);
        }
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        VH vh;
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_fpage_type,parent,false);
            vh = new VH(convertView);
            convertView.setTag(vh);
        }else{
            vh = (VH)convertView.getTag();
        }
        TypeBean.DataBean dataBean = list.get(position);
        vh.tv.setText(dataBean.getName());
        String icon = dataBean.getIcon();
        if(!TextUtils.isEmpty(icon)){
            String url = URLUtils.PUBLIC_URL+icon;
            Picasso.with(context).load(url).into(vh.iv);
        }
        convertView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                int id = dataBean.getId();
                Intent intent = new Intent(context,CategoryActivity.class);
                intent.putExtra("id",id);
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    class VH{
        ImageView iv;
        TextView tv;
        public VH(View view){
            iv = view.findViewById(R.id.item_fpage_type_iv);
            tv = view.findViewById(R.id.item_fpage_type_tv);
        }
    }
}
